<!--机构奖金数据 机构-考核结果 -->
<template>
  <basic-container class="bonus">
    <flexible :leftCols="leftCols" :removeHeight="100" ref="flexibleRef">
      <template slot="left">
        <div class="search-form">
          <el-button type="primary"  @click="asyncAssessData" >同步考核</el-button>
        </div>
        <!--左侧机构树组件-->
        <org-tree-by-dept ref="orgTreeByDept" @sendCurrentOrg="getCurrentOrg" :height="290"></org-tree-by-dept>
      </template>
      <template slot="right">
<!--        <div class="group-title">考核项管理</div>-->
        <div class="search-form">
          <el-form :inline="true" :model="searchForm" >
            <el-form-item label="考核项">
              <el-input v-model="searchForm.assessItem" placeholder="考核项" clearable></el-input>
            </el-form-item>
            <el-form-item label="考核时间">
              <el-date-picker type="date" placeholder="选择考核时间" v-model="searchForm.assessDate" format="yyyy-MM-dd"  value-format="yyyy-MM-dd" style="width: 100%;" clearable></el-date-picker>
            </el-form-item>
          </el-form>
          <div>
            <el-button type="primary" @click="handleSearch" :disabled="currentOrg==''">查询</el-button>
          </div>
        </div>
        <div class="mb10">
          <el-button type="primary" @click="add" :disabled="currentOrg==''">新增</el-button>
          <el-button type="danger" plain @click="del(true)" :disabled="currentOrg==''">删除</el-button>
          <el-button type="primary" plain @click="exportData" :disabled="currentOrg==''">导入</el-button>
        </div>
        <el-table
          v-loading="tableLoading"
          :data="tableData"
          @selection-change="handleSelectionChange"
          height="calc(100vh - 325px)"
          border
          style="width: 100%">
          <el-table-column
            type="selection"
            align="center"
            width="55">
          </el-table-column>
          <el-table-column width="55" label="序号" align="center" header-align="center">
            <template slot-scope="scope">
              {{(page.currentPage - 1) * page.pageSize + scope.$index + 1}}
            </template>
          </el-table-column>
          <el-table-column
            prop="deptName"
            label="机构名称"
            show-overflow-tooltip
            min-width="150">
          </el-table-column>
          <el-table-column
            prop="deptCode"
            label="机构编号"
            show-overflow-tooltip
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="deptRatio"
            width="150"
            show-overflow-tooltip
            align="center"
            header-align="center"
            label="机构（单位）系数">
          </el-table-column>
          <el-table-column
            prop="assessItem"
            min-width="150"
            header-align="center"
            align="center"
            show-overflow-tooltip
            label="考核项">
          </el-table-column>
          <el-table-column
            prop="assessDate"
            width="110"
            header-align="center"
            align="center"
            show-overflow-tooltip
            label="考核时间">
          </el-table-column>
          <el-table-column
            prop="assessScore"
            width="90"
            header-align="center"
            align="center"
            show-overflow-tooltip
            label="考核得分">
          </el-table-column>
          <el-table-column
            prop="assessGrade"
            width="90"
            header-align="center"
            align="center"
            show-overflow-tooltip
            label="考核评级">
          </el-table-column>
          <el-table-column prop="enableStatus" label="启用状态" min-width="140" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="handleSaveRowEdit(scope.row,scope.row.enableStatus)"
                v-model="scope.row.enableStatus"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="启用"
                inactive-text="停用"
                :disabled="scope.row.enableStatus=='已使用'"
                :active-value="0"
                :inactive-value="1">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column align="center" width="140px" label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="handleRow('edit',scope.row)">编辑</el-button>
              <el-button type="text" @click="handleRow('view',scope.row)">查看</el-button>
              <el-button type="text"  class="delete_btn" @click="del(false,scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            :total="page.total"
            background
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </template>
    </flexible>

    <!--添加人员-->
    <assessment-form ref="assessmentFormRef" @refresh="refreshTable"></assessment-form>

    <!--同步考核数据-->
    <async-assess-data-form ref="asyncAssessDataFormRef"></async-assess-data-form>

  </basic-container>
</template>

<script>
import flexible from "@/views/bonusmanage/components/flexible";
import assessmentForm from "@/views/bonusmanage/data/org/assessment/assessmentForm";
import {fetchList,delBatch,putObj} from '@/api/bonus/dataorgassessment'
import orgTreeByDept from "@/views/bonusmanage/components/orgTreeByDept";
import asyncAssessDataForm from "@/views/bonusmanage/components/asyncAsscessDataForm"
export default {
  name: "index",
  components:{
    orgTreeByDept,
    assessmentForm,
    flexible,
    asyncAssessDataForm
  },
  watch: {

  },
  computed: {

  },
  data(){
    return{
      treeLoading:false,
      tableLoading:false,
      leftCols:6,
      searchForm: {},
      multipleSelection:[],
      page:{
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      currentOrg:'',
    }
  },
  created(){
    this.$nextTick(()=>{
      this.$refs.orgTreeByDept.getJiangJinJg() ;
    })
  },
  mounted() {
  },
  methods: {
    handleSelectionChange(list){
      this.multipleSelection = list ;
      this.ids = this.multipleSelection.map((ele) => {
        return ele.id
      })
    },
    refreshTable(){
      this.onLoad() ;
    },
    getCurrentOrg(item){
      console.log("")
      this.currentOrg = item //当前点击的机构
      this.onLoad() ;
    },
    add(){
      if (JSON.stringify(this.currentOrg) == '{}'){
        this.$message.error('请选择机构')
        return
      }
      this.$refs.assessmentFormRef.init('add',{},this.currentOrg)
    },
    //删除
    del(batch,item){
      let msg = '' ;
      let ids = ''
      if (batch){ //批量
        msg = '确定删除选中的数据吗?'
        if (this.multipleSelection.length == 0){
          this.$message.error('请勾选要删除的数据')
          return ;
        }
        ids = this.ids.toString() ;
      }else{ //单条
        msg = '确定删除此条数据吗?'
        ids = item.id ;
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.treeLoading = true ;
        delBatch({ids:ids}).then(res=>{
          this.$message.success('操作成功')
          this.onLoad();
        }).catch(()=>{
          this.tableLoading = false ;
        })
      }).catch(() => {

      });
    },
    //编辑
    handleRow(type,item){
      this.$refs.assessmentFormRef.init(type,JSON.parse(JSON.stringify(item)))
    },
    handleSearch(){
      this.page.currentPage = 1;
      this.onLoad() ;
    },
    onLoad(){
      this.tableLoading = true ;
      fetchList(Object.assign({
        current: this.page.currentPage,
        size: this.page.pageSize
      },{bonusDeptId:this.currentOrg.bonusDeptId},this.searchForm)).then(res=>{
        if (res.data.code == 0){
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total ;
          this.page.currentPage = res.data.data.current ;
        }else{
          this.$message.error(res.data.msg)
        }
        this.tableLoading = false ;
      }).catch(()=>{
        this.tableLoading = false ;
      })
    },

    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.currentPage = 1;
      this.onLoad() ;
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val;
      this.onLoad() ;
    },
    exportData(){
      alert('导入功能待开发')
    },
    asyncAssessData(){
      this.$refs.asyncAssessDataFormRef.init('jg') ;
    },
    //保存行编辑
    handleSaveRowEdit(row) {
      putObj(row).then(res => {
        if (res.data.code == 0){
          row.enableStatus == 1 ? this.$message.success('停用成功') : this.$message.success('启用成功')
        }else{ //修改失败后
          row.enableStatus == 1 ? row.enableStatus = 0 : row.enableStatus = 1
          row.enableStatus == 1 ? this.$message.error('停用失败') : this.$message.error('启用失败')
        }
      }).catch(() => {
        row.enableStatus == 1 ? row.enableStatus = 0 : row.enableStatus = 1
      });
    }
  }
}
</script>

<style scoped>

</style>

